<?php
class MModule
{
	public function index()
	{
		global $_FANWE;
		$cache_args = array(
			'index',
			$_FANWE['cookie']['city_py'],
			intval($_FANWE['request']['cat']),
			trim($_FANWE['request']['sort']),
			$_FANWE['page']
		);

		//$_FANWE['nav_title'] = lang('common','index_index');

		$cache_file = getTplCache('mobile/index_index',$cache_args);

		define('IS_CACHE_TPL',true);

		if(!@include($cache_file))
			MModule::getList();

		display($cache_file);
	}

	public function today()
	{
		global $_FANWE;

		$cache_args = array(
			'today',
			$_FANWE['cookie']['city_py'],
			intval($_FANWE['request']['cat']),
			trim($_FANWE['request']['sort']),
			$_FANWE['page']
		);

		//$_FANWE['nav_title'] = lang('common','index_today');

		$cache_file = getTplCache('mobile/index_index',$cache_args);

		define('IS_CACHE_TPL',true);

		if(!@include($cache_file))
		{
			$today_time= getTodayTime();
			$where = " AND g.begin_time > '$today_time'";
			MModule::getList($where);
		}

		display($cache_file);
	}

	public function citys()
	{
		global $_FANWE;
		$cache_args = array(
			'citys',
			$_FANWE['cookie']['city_py'],
		);

		$cache_file = getTplCache('mobile/city_list',$cache_args);

		if(!@include($cache_file))
			include template('mobile/city_list');

		display($cache_file);
	}

	public function show()
	{
		global $_FANWE;
		$id = intval($_FANWE['request']['id']);
		if($id == 0)
			fHeader('location:m.php');

		S("RankStatistic")->goods($id,'GOODS_CLICK');

		$cache_args = array(
			$id,
		);

		$cache_file = getTplCache('mobile/goods_show',$cache_args);

		if(!@include($cache_file))
		{
			$goods = DB::fetchFirst('SELECT *,(shop_price / market_price) AS discount FROM '.DB::table('goods').' WHERE id = '.$id.' AND status = 1');

			if(!$goods)
				fHeader('location:m.php');

			$_FANWE['nav_title'] = $goods['name'];
			S('Goods')->goodsFormat($goods);
			$goods['site'] = $_FANWE['cache']['site']['all']['s_'.$goods['site_id']];
			include template('mobile/goods_show');
		}

		display($cache_file);
	}

	private function getList($where='',$no_query = false)
	{
		global $_FANWE;

		if(!$no_query)
		{
			if(defined('IS_CACHE_TPL'))
				$fields = '';
			else
				$fields = ',g.post_count';

			$page_args = array(
				'cat' => intval($_FANWE['request']['cat']),
				'sort' => trim($_FANWE['request']['sort']),
			);

			$insert_sort = 'gk.cr_sort ASC';
			
			if($page_args['cat'] > 0)
				$insert_sort = 'gk.dc_sort ASC';

			$city_name = $_FANWE['current_city']['name'];

			if(CURRENT_CITY_PY == CHINA_CITY_PY)
			{
				//全国不包含所有城市商品时
				if($_FANWE['setting']['all_city'] == 0)
					$where.=  " AND gk.city = '$city_name'";
				else
				{
					$insert_sort = 'gk.r_sort ASC';
					
					if($page_args['cat'] > 0)
						$insert_sort = 'gk.d_sort ASC';
				}
			}
			elseif(CURRENT_CITY_PY != '')
			{
				//城市显示全国商品时
				if($_FANWE['setting']['index_all'] == 1)
				{
					$fields .= ",(gk.city = '$city_name') as is_now_city";
					$insert_sort = 'is_now_city DESC,'.$insert_sort;
					$quanguo_name = $_FANWE['cache']['city']['all'][CHINA_CITY_PY]['name'];
					$where.=  " AND gk.city IN('$city_name','$quanguo_name')";
				}
				else
				{
					$where.=  " AND gk.city = '$city_name'";
				}
			}
			
			loadCache('goods');
			foreach($_FANWE['cache']['goods']['root_cates'] as $root_id)
			{
				$goods_cates['list'][] = $_FANWE['cache']['goods']['cats'][$root_id];
			}
			

			$cate_id = 0;
			if($page_args['cat'] > 0)
			{
				$cate_id = &$page_args['cat'];
				$cateobj = $_FANWE['cache']['goods']['cats'][$cate_id];
				$cate_ids = array();
				if(isset($cateobj['childs']))
					$cate_ids = $cateobj['childs'];
				
				$cate_ids[] = $cate_id;
				
				if(count($cate_ids) > 1)
					$where.=' AND gk.cate_id IN ('.implode(',', $cate_ids).')';
				else
					$where.=" AND gk.cate_id = '$cate_id'";
			}
			else
				unset($page_args['cat']);

			$order = '';
			$sort = &$page_args['sort'];
			$sort_array = array('spa'=>'g.shop_price ASC','spd'=>'g.shop_price DESC','eta'=>'g.end_time ASC','etd'=>'g.end_time DESC','cca'=>'g.click_count ASC','ccd'=>'g.click_count DESC','sa'=>'g.score ASC','sd'=>'g.score DESC','ba'=>'g.bought ASC','bd'=>'g.bought DESC','da'=>'discount ASC','dd'=>'discount DESC');

			if(array_key_exists($sort,$sort_array))
				$order = $sort_array[$sort];
			else
			{
				unset($page_args['sort']);
				$sort = '';
			}

			if(empty($order))
				$order .= 'g.is_best DESC,'.$insert_sort.',g.sort ASC,g.id ASC';
			else
				$order .= ','.$insert_sort.',g.sort ASC,g.id DESC';

			$goods_count = S('Goods')->getGoodsNowCount($where);

			$pager = buildPage('m/'.ACTION_NAME,$page_args,$goods_count,$_FANWE['page'],10,'',2);
			$goods_result = S('Goods')->getGoodsNowList($where,$order,$fields,$pager['limit']);
			$goods_result['ids'] = implode(',',$goods_result['ids']);
		}

		include template('mobile/index_index');
	}
}
?>